using System;
using System.Collections;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;

namespace CMS.Data
{
    public class KiemTraQuyenDao : IKiemTraQuyenDao
    {
        const Int32 DONTHUDEL = 1;
        const Int32 DANHMUCDEL = 2;
        const Int32 TIEPDANDEL = 3;
        const Int32 CONGTTDEL = 4;

        #region Create Instance
        private static KiemTraQuyenDao instance = null;
        public static KiemTraQuyenDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new KiemTraQuyenDao();
                }
                return instance;
            }
        }
        #endregion     
        
        bool CheckQuyen(string ChucNangs, int cn)
        { 
            bool result = false;
            if (ChucNangs != null && !ChucNangs.Equals(String.Empty))
            {
                foreach (string value in ChucNangs.Split(new Char[] { ',' }))
                {
                    if (Convert.ToByte(value) == cn)
                        result = true;
                }
            }
            return result;
        }
    
        #region IKiemTraQuyenDao Members

        public bool CheckQuyenHuyDonThu(string MaNguoiSuDung)
        {
            bool result = false;
            try
            {
                NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
                if (nsd != null)
                {
                    result = this.CheckQuyen(nsd.ChucNang, DONTHUDEL);
                    if (!result)
                    {
                        NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
                        result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, DONTHUDEL);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }
        public bool CheckQuyenHuyDanhMuc(string MaNguoiSuDung)
        {
            bool result = false;
            try
            {
                NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
                if (nsd != null)
                {
                    result = this.CheckQuyen(nsd.ChucNang, DANHMUCDEL);
                    if (!result)
                    {
                        NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
                        result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, DANHMUCDEL);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }
        public bool CheckQuyenHuyTiepDan(string MaNguoiSuDung)
        {
            bool result = false;
            try
            {
                NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
                if (nsd != null)
                {
                    result = this.CheckQuyen(nsd.ChucNang, TIEPDANDEL);
                    if (!result)
                    {
                        NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
                        result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, TIEPDANDEL);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }
        public bool CheckQuyenHuyCongTT(string MaNguoiSuDung)
        {
            bool result = false;
            try
            {
                NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
                if (nsd != null)
                {
                    result = this.CheckQuyen(nsd.ChucNang, CONGTTDEL);
                    if (!result)
                    {
                        NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
                        result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, CONGTTDEL);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }
        //-- se bo
        //public bool CheckQuyenCapNhatNguoiSuDung(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 1);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 1);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenCapNhatThongTinDanhMuc(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 2);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 2);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenCapNhatVanBanDenDi(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 3);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 3);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenThongKeInAn(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 4);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 4);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenCapNhatVBDuThao(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 5);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 5);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenTaoLapChuyenGiaoCongViec(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 6);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 6);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenCapNhatHSCV(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 7);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 7);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        //public bool CheckQuyenCapNhatVBNoiBo(string MaNguoiSuDung)
        //{
        //    bool result = false;
        //    try
        //    {
        //        NguoiSuDung nsd = NguoiSuDungDao.Instance.GetById(MaNguoiSuDung);
        //        if (nsd != null)
        //        {
        //            result = this.CheckQuyen(nsd.ChucNang, 8);
        //            if (!result)
        //            {
        //                NHibernate.NHibernateUtil.Initialize(nsd.NhomNguoiSuDung);
        //                result = this.CheckQuyen(nsd.NhomNguoiSuDung.ChucNang, 8);
        //            }
        //        }
        //        return result;
        //    }
        //    catch(Exception ex)
        //    {
        //        return result;
        //    }
        //}

        #endregion
    }
}
